{include file="common/header" /}

<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">栏目名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="catname" value="{:input('param.catname')}" autocomplete="off" class="layui-input" >
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <div class="layuimini-container" style="margin-top: 10px;">
            <div class="layuimini-main">
                <div>
                    <div class="layui-btn-group">
                        <a href="{:url(CONTROLLER_NAME.'/add')}"><button class="layui-btn layui-btn-sm" >添加</button></a>
                    </div>
                </div>
            </div>
        </div>

        <table class="layui-table" lay-size="sm">
          <colgroup>
            <col width="80">
            <col>
            <col width="120">
            <!-- <col width="120"> -->
            <col width="180">
            <!-- <col width="120"> -->
            <col width="120">
            <col width="120">
            <col width="80">
            <col>
          </colgroup>
          <thead>
            <tr>
                <th>ID</th>
                <th>栏目名称</th>
                <th>图片</th>
                
                <th>是否开启</th>
                <th>是否前台展示</th>
                <th>排序</th>
                <th>操作</th>
            </tr> 
          </thead>
          <tbody>

            {foreach name="$treeList" item="vo" key="k" }
            <tr>
                <td>{$vo.id}</td>
                <td>{$vo.catname | raw} ({$vo.level}级)</td>

                <td>
                    {if $vo.picurl /}
                    <a href="{$vo.picurl}" target="_blank" ><img src="{$vo.picurl}" style="width: 40px;"></a>
                    {/if}
                </td>

                
                <td class="layui-form">
                    <input type="checkbox" name="is_open" data-table="{:CONTROLLER_NAME}" data-id_name="id" data-id="{$vo.id}" data-field="is_open" lay-skin="switch" lay-text="是|否" lay-filter="switchIsOpen" {if $vo.is_open == 1 /} checked {/if}>
                </td>

                <td class="layui-form">
                    <input type="checkbox" name="is_show" data-table="{:CONTROLLER_NAME}" data-id_name="id" data-id="{$vo.id}" data-field="is_show" lay-skin="switch" lay-text="是|否" lay-filter="switchIsOpen" {if $vo.is_show == 1 /} checked {/if}>
                </td>

                <td ><input type="number" name="" onBlur="checkSort(this)" data-table="Category" data-id_name="id" data-id="{$vo.id}" data-field="orderid" value="{$vo.orderid}" style="width:40px;"></td>
                <td>
                    <div >
                        
                        {if $vo.level == 1 || $vo.level == 2 /}
                        <a class="layui-btn layui-btn-xs " href="{:url(CONTROLLER_NAME.'/add',array('id'=>$vo['id'],'module'=>$vo['module']))}">添加子栏目</a>
                        {/if}

                        <a class="layui-btn layui-btn-xs " href="{:url(CONTROLLER_NAME.'/edit',array('id'=>$vo['id']))}">编辑</a>
                        <a class="layui-btn layui-btn-xs layui-btn-danger"  href="javascript:void(0)" data-url="{:url(CONTROLLER_NAME.'/delete')}" data-id="{$vo.id}" onClick="delfun(this)">删除</a>

                        {if $vo.level !=1 /}
                        <a class="layui-btn layui-btn-xs " href="{$vo.module_url}">添加内容</a>
                        {/if}
                    </div>
                </td>
            </tr>
            {/foreach}
          </tbody>
        </table>

    </div>
</div>

<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layuimini = layui.layuimini;


            //改变排序
            window.checkSort=function(obj){
                let table = $(obj).attr("data-table");
                let id_name = $(obj).attr("data-id_name");
                let id = $(obj).attr("data-id");
                let val = $(obj).val();
                let field = $(obj).attr("data-field");

                $.ajax({
                    url:"{:url('Category/changeSort')}",
                    data: {'table':table,'id_name':id_name,'id':id,'field':field,'val':val},
                    type:'post',
                    dataType:'json',
                    success:function(res){
                        // console.log(res);
                        // return false;
                        if(res.errcode == 1){
                            layer.msg(res.message,{icon: 1,time: 1000},function(){
                                location.reload();
                            })
                        }else {
                            layer.msg(res.message,{icon: 2,time: 2000})
                        }
                        // parent.layer.close(index);
                    },
                    error : function(XMLHttpRequest, textStatus, errorThrown) {
                        layer.msg('网络失败，请刷新页面后重试!',{icon: 2,time: 2000})
                    }
                });
            };


            window.delfun = function(obj){
                // 删除按钮
                layer.confirm('确认删除？', {
                    title:'温馨提示',closeBtn:0,icon:3,
                    btn: ['确定', '取消'] //按钮
                }, function () {
                    $.ajax({
                        type: 'post',
                        url: $(obj).attr('data-url'),
                        data: {id: $(obj).attr('data-id')},
                        dataType: 'json',
                        success: function (res) {
                            // console.log(res)
                            // return false
                            layer.closeAll();
                            if (res.errcode == 1) {
                                layer.msg(res.message,{icon: 1,time: 1000},function(){
                                    //$(obj).parent().parent().parent().remove();
                                    location=location;
                                })
                            } else {
                                layer.msg(res.message,{icon: 2,time: 2000})
                                //layer.alert(data, {icon: 2});  //alert('删除失败');
                            }
                        }
                    })
                }, function () {
                    layer.closeAll();
                });
            };



        table.render({
            elem: '#currentTableId',
            url: "{:url('lists/list')}",
            toolbar: '#toolbarDemo',
            defaultToolbar: [],
            /*defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],*/
            cols: [[
                // {type: "checkbox", width: 50, fixed: "left"},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'cates_name', width:150,  title: '所属分类'},
                {field: 'title',  title: '标题'},
                {field: 'img_url',width: 80, title: '图片',align:'center',templet:function(res){
                    var html = '';
                    html += '<a href="'+res.img_url+'" target="_blank" ><img style="width:50px;height:50px;" src="'+res.img_url+'"></a>';
                    return html;
                }},
                {field: 'price', width: 80, title: '价格'},
                {field: 'duration', width: 80, title: '时长'},
                {field: 'effective_date', width: 120, title: '有效期'},

                {field: 'is_open', width: 100, title: '是否开启', templet: '#is_open'},
                {field: 'add_time', width: 135, title: '添加时间', sort: true},
                {title: '操作', width: 150, templet: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true
        });

        //是否开启
        form.on('switch(switchIsOpen)', function(data){
            var checked = data.elem.checked;
            //var role_id = data.elem.value;
            let table = $(data.elem).attr("data-table");
            let id_name = $(data.elem).attr("data-id_name");
            let id = $(data.elem).attr("data-id");
            let field = $(data.elem).attr("data-field");
            console.log(table)
            // var id = data.elem.attributes['switch_id'].nodeValue;
            // var pri_id = data.elem.attributes['switch_pri_id'].nodeValue;
            // var tempId = data.elem.attributes['switch_tempId'].nodeValue;

            $.ajax({
                url:"{:url(CONTROLLER_NAME.'/changeStatus')}",
                data: {'table':table,'checked':checked,'id_name':id_name,'id':id,'field':field},
                type:'post',
                dataType:'json',
                success:function(res){
                    // console.log(res);
                    // return false;
                    if(res.errcode == 1){
                        layer.msg(res.message,{icon: 1,time: 1000},function(){
                            location.reload();
                        })
                    }else {
                        layer.msg(res.message,{icon: 2,time: 2000})
                    }
                    // parent.layer.close(index);
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('网络失败，请刷新页面后重试!',{icon: 2,time: 2000})
                }
            });
           
            
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //var result = JSON.stringify(data.field);
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });
            table.reload('currentTableId',{
                where: data.field
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            })

            return false;
        });

        // 监听添加操作
        $(".data-add-btn").on("click", function () {

            layerOpenWindow("{:url('role/add')}","添加用户",['80%', '80%'],true);

            return false;
        });

        /*// 监听删除操作
        $(".data-delete-btn").on("click", function () {
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
                console.log(data);

            var ids = '';
            if (data.length == 0) {
                layer.msg("请选择数据",{icon:2})
                return false
            } else {
                for (var i = 0; i < data.length; i++) {
                    ids += data[i].admin_id + ',';
                }
                ids=ids.substring(0,ids.length-1);
            }
            console.log(ids)

            $.ajax({
                url:"{:url('admin/deleteMore')}",
                data: {ids:ids},
                type:'post',
                dataType:'json',
                success:function(res){
                    if(res.errcode == 1){
                        layer.msg(res.message,{icon: 1,time: 1000},function(){
                            location.reload();
                        })
                    }else {
                        layer.msg(res.message,{icon: 2,time: 2000})
                    }
                    // parent.layer.close(index);
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('网络失败，请刷新页面后重试!',{icon: 2,time: 2000})
                }
            });
        });
*/
        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                /*layerOpenWindow("{:url('admin/edit')}?admin_id="+data.admin_id,"修改用户",['80%', '80%'],true);
                $(window).on("resize", function () {
                    layer.full(index);
                });*/
                window.location.href="{:url('lists/edit')}?id="+data.id+"&type=1";
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确认删除吗', function (index) {
                    
                    $.ajax({
                        url:"{:url('lists/delete')}",
                        data: {id:data.id,'table':'lists','id_name':'id'},
                        type:'post',
                        dataType:'json',
                        success:function(res){
                            // console.log(res);
                            // return false
                            if(res.errcode == 1){
                                layer.msg(res.message,{icon: 1,time: 1000},function(){
                                    obj.del(); //删除对应行（tr）的DOM结构
                                    location.reload();
                                })
                            }else {
                                layer.msg(res.message,{icon: 2,time: 2000})
                            }
                            // parent.layer.close(index);
                        },
                        error : function(XMLHttpRequest, textStatus, errorThrown) {
                            layer.msg('网络失败，请刷新页面后重试!',{icon: 2,time: 2000})
                        }
                    });

                });
            }
        });

    });
</script>

</body>
</html>